﻿package com.athena.process
{
	import flash.display.MovieClip;
	import flash.display.*;
	import flash.utils.*;
	import flash.events.*;
	import flash.text.TextFormat;
	import com.adobe.serialization.json.JSON;
	/**
	 * ...
	 * @author ...
	 */
	public class LockFunction 
	{
		public static var forcus : int = 0 ;
		
		public static function UnAddToText(str : String) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("UnLock")) ;
			if (popup == null)
				popup = MovieClip(Cocaro.mainStage_mc.getChildByName("DeleteLock")) ;
			if (popup == null) return ;
			popup.Key_txt.text += str ;			
		}
		public static function UnLock0_Click(evt : MouseEvent) {
			UnAddToText("0") ;
		}
		public static function UnLock1_Click(evt : MouseEvent) {
			UnAddToText("1") ;	
		}
		public static function UnLock2_Click(evt : MouseEvent) {
			UnAddToText("2") ;	
		}
		public static function UnLock3_Click(evt : MouseEvent) {
			UnAddToText("3") ;	
		}
		public static function UnLock4_Click(evt : MouseEvent) {
			UnAddToText("4") ;	
		}
		public static function UnLock5_Click(evt : MouseEvent) {
			UnAddToText("5") ;	
		}
		public static function UnLock6_Click(evt : MouseEvent) {
			UnAddToText("6") ;	
		}
		public static function UnLock7_Click(evt : MouseEvent) {
			UnAddToText("7") ;	
		}
		public static function UnLock8_Click(evt : MouseEvent) {
			UnAddToText("8") ;	
		}
		public static function UnLock9_Click(evt : MouseEvent) {
			UnAddToText("9") ;	
		}
		public static function UnLock10_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("UnLock")) ;
			if (popup == null)
				popup = MovieClip(Cocaro.mainStage_mc.getChildByName("DeleteLock")) ;
			if (popup == null) return ;
			popup.Key_txt.text = "" ;				
		}
		public static function FormUnLock() {
			var popup : MovieClip = MovieClip(new Popup_UnLock()) ;
			popup.Lock0_btn.addEventListener(MouseEvent.CLICK, UnLock0_Click) ;
			popup.Lock1_btn.addEventListener(MouseEvent.CLICK, UnLock1_Click) ;
			popup.Lock2_btn.addEventListener(MouseEvent.CLICK, UnLock2_Click) ;
			popup.Lock3_btn.addEventListener(MouseEvent.CLICK, UnLock3_Click) ;
			popup.Lock4_btn.addEventListener(MouseEvent.CLICK, UnLock4_Click) ;
			popup.Lock5_btn.addEventListener(MouseEvent.CLICK, UnLock5_Click) ;
			popup.Lock6_btn.addEventListener(MouseEvent.CLICK, UnLock6_Click) ;
			popup.Lock7_btn.addEventListener(MouseEvent.CLICK, UnLock7_Click) ;
			popup.Lock8_btn.addEventListener(MouseEvent.CLICK, UnLock8_Click) ;
			popup.Lock9_btn.addEventListener(MouseEvent.CLICK, UnLock9_Click) ;
			popup.Lock10_btn.addEventListener(MouseEvent.CLICK, UnLock10_Click) ;
			popup.OpenKey_btn.addEventListener(MouseEvent.CLICK, Unlock_Click) ;
			popup.Unlock_btn.addEventListener(MouseEvent.CLICK, UnlockWait_Click) ;
			var tfStyle:TextFormat = new TextFormat();
			tfStyle.size = 20;
			tfStyle.color = 0xFFFFFF;
			tfStyle.font = "Arial";
			popup.Key_txt.setStyle("textFormat", tfStyle);
			popup.name = "UnLock" ;
			if (Cocaro.currPlayer.Lock == 8) {
				popup.Unlock_btn.visible = true ;
			} else {
				popup.Unlock_btn.visible = false ;
				popup.Day_txt.text = Cocaro.currPlayer.Lock ;
			}
			popup.x = 0 ;
			popup.y = 0 ;
			Cocaro.mainStage_mc.addChild(popup) ;
		}
		public static function UnlockWait_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("UnLock")) ;
			if (popup == null) return ;
			var obj : Object = new Object() ;
			obj.evt = "12" ; //Mokhoa
			obj.T = "2" ; //Mo khoa sau 7 ngay
			obj.P = popup.Key_txt.text ;
			//Cocaro.tf.appendText("=>SendULW:" + JSON.encode(obj)) ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;			
		}
		public static function UnLockWaitOK() { //Server gui ve bao unlock sau 7 ngay OK
			//Cocaro.tf.appendText("==>DeleLockWaitOK") ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("UnLock")) ;
			if (popup == null) return ;
			popup.Unlock_btn.visible = false ;
			popup.Day_txt.text = "7" ;
			Cocaro.currPlayer.Lock = 7 ;
		}
		public static function Unlock_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("UnLock")) ;
			if (popup == null) return ;
			var obj : Object = new Object() ;
			obj.evt = "12" ; //Mokhoa
			obj.T = "5" ; //Mo khoa ngay lap tuc, khong dung khoa nua
			obj.P = popup.Key_txt.text ;
			//Cocaro.tf.appendText("=>SendUL:" + JSON.encode(obj)) ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;			
		}
		public static function UnLockOK() { //Mo mat khau thanh cong
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("UnLock")) ;
			if (popup == null) return ;
			Cocaro.mainStage_mc.removeChild(popup) ;
			if (Cocaro.kickProfile) {
				Cocaro.kickProfile = false ;
				ProfileFunction.Show_Popup_Profile() ;
			}
		}
		/****************************************************************************/
		public static function CreateLockOK() { //Server bao tao mat khau thanh cong
			//Cocaro.tf.appendText("==>CreateLockOK") ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lock")) ;
			if (popup == null) return ;
			Cocaro.currPlayer.Lock = 8 ;
			BasicFunction.ShowAlert("Bạn tạo mật khẩu thành công!") ;
			Cocaro.mainStage_mc.removeChild(popup) ;
		}
		public static function Lock_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(new Popup_Lock()) ;
			popup.Lock0_btn.addEventListener(MouseEvent.CLICK, Lock0_Click) ;
			popup.Lock1_btn.addEventListener(MouseEvent.CLICK, Lock1_Click) ;
			popup.Lock2_btn.addEventListener(MouseEvent.CLICK, Lock2_Click) ;
			popup.Lock3_btn.addEventListener(MouseEvent.CLICK, Lock3_Click) ;
			popup.Lock4_btn.addEventListener(MouseEvent.CLICK, Lock4_Click) ;
			popup.Lock5_btn.addEventListener(MouseEvent.CLICK, Lock5_Click) ;
			popup.Lock6_btn.addEventListener(MouseEvent.CLICK, Lock6_Click) ;
			popup.Lock7_btn.addEventListener(MouseEvent.CLICK, Lock7_Click) ;
			popup.Lock8_btn.addEventListener(MouseEvent.CLICK, Lock8_Click) ;
			popup.Lock9_btn.addEventListener(MouseEvent.CLICK, Lock9_Click) ;
			popup.Lock10_btn.addEventListener(MouseEvent.CLICK, Lock10_Click) ;
			popup.OldPass_txt.addEventListener(MouseEvent.CLICK, OldPass_Click) ;
			popup.Pass_txt.addEventListener(MouseEvent.CLICK, Pass_Click) ;
			popup.RePass_txt.addEventListener(MouseEvent.CLICK, RePass_Click) ;
			popup.Xacnhankhoa_btn.addEventListener(MouseEvent.CLICK, Xacnhankhoa_Click) ;
			popup.Huykhoa_btn.addEventListener(MouseEvent.CLICK, Huykhoa_Click) ;
			popup.Close_btn.addEventListener(MouseEvent.CLICK, Close_Click) ;
			//Cocaro.tf.appendText("=>CurentLock:" + Cocaro.currPlayer.Lock) ;
			var tfStyle:TextFormat = new TextFormat();
			tfStyle.size = 20;
			tfStyle.color = 0xFFFFFF;
			tfStyle.font = "Arial";
			popup.OldPass_txt.setStyle("textFormat", tfStyle);
			popup.Pass_txt.setStyle("textFormat", tfStyle);
			popup.RePass_txt.setStyle("textFormat", tfStyle);
			if (Cocaro.currPlayer.Lock == 0) {
				popup.OldPass_txt.visible = false ;				
				popup.OldPass_Focus_mc.visible = false ;
				popup.OldLabel_txt.visible = false ;	
				popup.Pass_Focus_mc.visible = true ;
				popup.RePass_Focus_mc.visible = false ;
				popup.Xacnhankhoa_btn.x = 440 ;
				popup.Huykhoa_btn.visible = false ;
				forcus = 1 ;
			} else {
				popup.OldPass_txt.visible = true ;				
				popup.OldPass_Focus_mc.visible = true;
				popup.OldLabel_txt.visible = true ;	
				popup.OldLabel_txt.selectable = false ;
				popup.Pass_Focus_mc.visible = false ;
				popup.RePass_Focus_mc.visible = false ;
				popup.Xacnhankhoa_btn.x = 350 ;
				popup.Huykhoa_btn.visible = true ;
				forcus = 0 ;
			}
			popup.name = "Lock" ;			
			popup.x = 0 ;
			popup.y = 0 ;
			Cocaro.mainStage_mc.addChild(popup) ;
		}
		public static function OldPass_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lock")) ;
			if (popup == null) return ;
			popup.OldPass_Focus_mc.visible = true ;
			popup.Pass_Focus_mc.visible = false ;
			popup.RePass_Focus_mc.visible = false ;
			forcus = 0 ;
		}
		public static function Pass_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lock")) ;
			if (popup == null) return ;
			popup.OldPass_Focus_mc.visible = false ;
			popup.Pass_Focus_mc.visible = true ;
			popup.RePass_Focus_mc.visible = false ;
			forcus = 1 ;
		}
		public static function RePass_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lock")) ;
			if (popup == null) return ;
			popup.OldPass_Focus_mc.visible = false ;
			popup.Pass_Focus_mc.visible = false ;
			popup.RePass_Focus_mc.visible = true ;
			forcus = 2 ;
		}
		public static function AddToText(str : String) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lock")) ;
			if (popup == null) return ;
			if (forcus == 0) {
				popup.OldPass_txt.text += str ;
			} else if (forcus == 1) {
				popup.Pass_txt.text += str ;
			} else if (forcus == 2) {
				popup.RePass_txt.text += str ;
			}
		}
		//evt = 12
		//T = 1 // Dat mat khau
		//T = 2 // Dat huy mat khau sau 3 ngay
		//T = 3 // Mo mat khau
		//T = 4 // Doi mat khau => NP, P
		//T = 5 // Mo mat khau
		//T = 7 // Huy mat khau ngay lap tuc		
		public static function Lock0_Click(evt : MouseEvent) {
			AddToText("0") ;
		}
		public static function Lock1_Click(evt : MouseEvent) {
			AddToText("1") ;	
		}
		public static function Lock2_Click(evt : MouseEvent) {
			AddToText("2") ;	
		}
		public static function Lock3_Click(evt : MouseEvent) {
			AddToText("3") ;	
		}
		public static function Lock4_Click(evt : MouseEvent) {
			AddToText("4") ;	
		}
		public static function Lock5_Click(evt : MouseEvent) {
			AddToText("5") ;	
		}
		public static function Lock6_Click(evt : MouseEvent) {
			AddToText("6") ;	
		}
		public static function Lock7_Click(evt : MouseEvent) {
			AddToText("7") ;	
		}
		public static function Lock8_Click(evt : MouseEvent) {
			AddToText("8") ;	
		}
		public static function Lock9_Click(evt : MouseEvent) {
			AddToText("9") ;	
		}
		public static function Lock10_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lock")) ;
			if (popup == null) return ;
			if (forcus == 0) {
				popup.OldPass_txt.text = "" ;
			} else if (forcus == 1) {
				popup.Pass_txt.text = "" ;
			} else if (forcus == 2) {
				popup.RePass_txt.text = "" ;
			}	
		}
		public static function Xacnhankhoa_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lock")) ;
			if (popup == null) return ;
			if (popup.Pass_txt.text != popup.RePass_txt.text) {
				BasicFunction.ShowAlert("Khóa lặp lại không trùng với khóa!") ;
				return ;
			}
			var obj : Object = new Object() ;
			obj.evt = "12" ;
			if (Cocaro.currPlayer.Lock == 0) {
				obj.T = 1 ;
				obj.P = popup.Pass_txt.text ;
			} else {
				obj.T = 4 ;
				obj.P = popup.OldPass_txt.text ;
				obj.NP = popup.Pass_txt.text ;
			}
			//Cocaro.tf.appendText("=>Send:" + JSON.encode(obj)) ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;			
		}
		public static function Close_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Lock")) ;
			if (popup == null) return ;
			Cocaro.mainStage_mc.removeChild(popup) ;
		}
		/******************* Huy Khoa ***********************************/
		public static function Huykhoa_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(new Popup_DeleteLock()) ;
			popup.Lock0_btn.addEventListener(MouseEvent.CLICK, UnLock0_Click) ;
			popup.Lock1_btn.addEventListener(MouseEvent.CLICK, UnLock1_Click) ;
			popup.Lock2_btn.addEventListener(MouseEvent.CLICK, UnLock2_Click) ;
			popup.Lock3_btn.addEventListener(MouseEvent.CLICK, UnLock3_Click) ;
			popup.Lock4_btn.addEventListener(MouseEvent.CLICK, UnLock4_Click) ;
			popup.Lock5_btn.addEventListener(MouseEvent.CLICK, UnLock5_Click) ;
			popup.Lock6_btn.addEventListener(MouseEvent.CLICK, UnLock6_Click) ;
			popup.Lock7_btn.addEventListener(MouseEvent.CLICK, UnLock7_Click) ;
			popup.Lock8_btn.addEventListener(MouseEvent.CLICK, UnLock8_Click) ;
			popup.Lock9_btn.addEventListener(MouseEvent.CLICK, UnLock9_Click) ;
			popup.Lock10_btn.addEventListener(MouseEvent.CLICK, UnLock10_Click) ;
			popup.Close_btn.addEventListener(MouseEvent.CLICK, CloseDeleteLock_Click) ;
			popup.Unlock_btn.addEventListener(MouseEvent.CLICK, DeleteLock_Click) ;
			popup.name = "DeleteLock" ;
			popup.x = 0 ;
			popup.y = 0 ;
			var tfStyle:TextFormat = new TextFormat();
			tfStyle.size = 20;
			tfStyle.color = 0xFFFFFF;
			tfStyle.font = "Arial";
			popup.Key_txt.setStyle("textFormat", tfStyle);				
			Cocaro.mainStage_mc.addChild(popup) ;
		}
		public static function CloseDeleteLock_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("DeleteLock")) ;
			if (popup == null) return ;
			Cocaro.mainStage_mc.removeChild(popup) ;
		}
		public static function DeleteLock_Click(evt : MouseEvent) {
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("DeleteLock")) ;
			if (popup == null) return ;
			if (popup.Key_txt.text == "") {
				BasicFunction.ShowAlert("Bạn phải nhập mật khẩu hiện tại!") ;
				return ;
			}
			var obj : Object = new Object() ;
			obj.evt = "12" ;
			obj.T = 7 ;
			obj.P = popup.Key_txt.text ;
			//Cocaro.tf.appendText("=>SendChangeP:" + JSON.encode(obj)) ;
			Cocaro.mainSocket.sendService(JSON.encode(obj)) ;
		}
		public static function DeleteLockOK() {
			//Cocaro.tf.appendText("==>DeleteLockOK") ;
			var popup : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("DeleteLock")) ;
			if (popup == null) return ;
			Cocaro.currPlayer.Lock = 0 ;
			Cocaro.mainStage_mc.removeChild(popup) ;			
			popup = MovieClip(Cocaro.mainStage_mc.getChildByName("Lock")) ;
			if (popup == null) return ;
			Cocaro.mainStage_mc.removeChild(popup) ;
			BasicFunction.ShowAlert("Bạn đã hủy khóa thành công!") ;
		}
	}
	
}